package com.thinkit.bigdata.web.controller.config;

import com.thinkit.bigdata.web.model.config.SysArea;
import com.thinkit.bigdata.web.model.config.SysAreaExample;
import com.thinkit.bigdata.web.model.result.Constants;
import com.thinkit.bigdata.web.model.sec.User;
import com.thinkit.bigdata.web.service.config.AreaService;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * Modify by Lihongli on 2018/09/25.
 */
@Controller
@RequestMapping(value = "area")
public class AreaController {

    private Logger logger = Logger.getLogger(AreaController.class);

    @Resource
    private AreaService areaService;

    /**
     * 查询所有区域信息
     * 如果用户是集团用户 则返回所有省份 否则只返回用户所在省份
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/showAllAreas")
    public List<SysArea> showAllAreas(HttpServletRequest request){
        logger.info("获取所有区域信息");
        // 判断当前登录用户身份
        User user = (User)SecurityUtils.getSubject().getSession().getAttribute(Constants.SESSION_USER_INFO);
        SysAreaExample example = new SysAreaExample();
        // 如果用户所在组织机构为空（超管）、或者不为空 areaid为设置的默认值（集团人员）
        if(user.getGroupid() == null || (user.getGroupid() != null && user.getGroup().getAreaid().equals(Constants.GROUP_AREAID))) {
            // 查询所有可用省份信息
            example.createCriteria().andEnabledEqualTo(1);
        }else{
            // 只查询用户所在省份信息
            example.createCriteria().andIdEqualTo(user.getGroup().getAreaid().intValue());
        }
        example.setOrderByClause("id asc");
        return areaService.selectByExample(example);
    }

    /**
     * 获取当前登录用户省份信息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getAreaInfo")
    public SysArea getAreaInfo(HttpServletRequest request){
        return (SysArea)SecurityUtils.getSubject().getSession().getAttribute("areaInfo");
    }

}
